package satir.framework;

import java.io.*;
import org.json.*;

/**
 * The Class JSONIOPerformer.
 * 
 * @version 1.0 February 2012
 * @author Chris Chan
 */
public class JSONIOPerformer {

    /** The json object. */
    private JSONObject jsonObject;
    
    /** The filename. */
    private String filename;

    /**
     * Instantiates a new jSONIO performer.
     *
     * @param filename the filename
     * @throws JSONIOPerformerReadException the read fail exception
     * @throws JSONIOPerformerJSONDecodeException the JSON decode fail exception
     */
    public JSONIOPerformer(String filename) throws JSONIOPerformerReadException, JSONIOPerformerJSONDecodeException {
        this.filename = filename;
        try {
            jsonObject = new JSONObject(read());
        } catch (JSONException e) {
            throw new JSONIOPerformerJSONDecodeException(filename);
        }
    }

    /**
     * Retrieve the JSON object.
     *
     * @return the jSON object
     */
    public JSONObject retrieve() {
        return jsonObject;
    }

    /**
     * Write.
     *
     * @throws JSONIOPerformerWriteException the jSONIO performer write exception
     */
    public void write() throws JSONIOPerformerWriteException {
        try {
            FileWriter fstream = new FileWriter(filename);
            BufferedWriter out = new BufferedWriter(fstream);
            out.write(jsonObject.toString());

        } catch (Exception e) {
            throw new JSONIOPerformerWriteException(filename);
        }
    }

    /**
     * Read the filename.
     *
     * @return the string
     * @throws JSONIOPerformerReadException the jSONIO performer read exception
     */
    private String read() throws JSONIOPerformerReadException {
        StringBuilder cardTxt = new StringBuilder();
        try {
            InputStreamReader inS = new InputStreamReader(
                    ClassLoader.getSystemResourceAsStream(filename));
            BufferedReader input = new BufferedReader(inS);
            String line;
            while ((line = input.readLine()) != null) {
                cardTxt.append(line.trim());
            }
        } catch (Exception e) {
            throw new JSONIOPerformerReadException(filename);
        }
        return cardTxt.toString();
    }

    /**
     * The Class JSONIOPerformerException.
     */
    public class JSONIOPerformerException extends Exception {

        /**
         * Instantiates a new jSONIO performer exception.
         *
         * @param message the message
         */
        public JSONIOPerformerException(String message) {
            super(message);
        }
    }

    /**
     * The Class JSONIOPerformerReadException.
     */
    public class JSONIOPerformerReadException extends JSONIOPerformerException {

        /**
         * Instantiates a new jSONIO performer read exception.
         *
         * @param filename the filename
         */
        public JSONIOPerformerReadException(String filename) {
            super("ERROR: Perform read file failure! Reference: filename[" + filename + "]");
        }
    }

    /**
     * The Class JSONIOPerformerWriteException.
     */
    public class JSONIOPerformerWriteException extends JSONIOPerformerException {

        /**
         * Instantiates a new jSONIO performer write exception.
         *
         * @param filename the filename
         */
        public JSONIOPerformerWriteException(String filename) {
            super("ERROR: Perform write file failure! Reference: filename[" + filename + "]");
        }
    }

    /**
     * The Class JSONIOPerformerJSONDecodeException.
     */
    public class JSONIOPerformerJSONDecodeException extends JSONIOPerformerException {

        /**
         * Instantiates a new jSONIO performer json decode exception.
         *
         * @param filename the filename
         */
        public JSONIOPerformerJSONDecodeException(String filename) {
            super("ERROR: The target file is not a JSON structure! Reference: filename[" + filename + "]");
        }
    }
}
